import { getPage, getPageDEle, getPagepublish } from '@/services';
import { IpageItem } from '@/types';
import { formatting } from '@/utils';
import { Effect, Reducer } from 'umi';
export interface PageModelState {
    pageDate: IpageItem[];
    pageLength: number;
    pageId:Partial<IpageItem>
}
export interface PageModelType {
    namespace: 'page';
    state: PageModelState;
    effects: {
        getPage: Effect;
        getPagepublish:Effect;
        getPageDEle:Effect;
    };
    reducers: {
        save: Reducer<PageModelState>;
    };
}
const PageModel: PageModelType = {
    namespace: 'page',
    state: {
        pageDate: [],
        pageLength: 0,
        pageId:{}
    },
    effects: {
        *getPage({ payload }, { call, put }) {
            let result = yield call(getPage,payload);   
            if (result.data) {
                yield put({
                    type: 'save',
                    payload: {
                        pageDate: result.data[0].map((item: IpageItem) => {
                            if(item.status==="draft"){
                                item.status="草稿"
                            }else{
                                item.status="已发布"
                            }
                            item.publishAt=formatting(item.publishAt)
                            item.key = item.id
                            return item
                        }),
                        pageLength: result.data[1]
                    }
                })
            }
        },
        *getPagepublish({payload},{call,put}){
            let result = yield call(getPagepublish,payload);
            if(result.data){
                yield put({
                    type:'save',
                    payload:{
                        pageId:result.data
                    }
                })
            }
        },
        *getPageDEle({payload},{call,put}){
            let result = yield call(getPageDEle,payload);
            if(result.data){
                yield put({
                    type:'save',
                    payload:{
                        pageId:result.data
                    }
                })
            }
        }
    },

    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload,
            };
        },
    },
};

export default PageModel;


